package arithmetic2;

/**
 * @author macpro
 * @date 2019-08-04
 * @function
 * easy
 * 滑动窗口一遍
 *
 */
public class T_121_BestTimeToBuyAndSellStock {
    public static void main(String[] args) {
        System.out.println(maxProfit(new int[]{7,1,5,3,6,4}));
    }

    public static int maxProfit(int[] prices){
        int maxProfit =0;
        if (prices == null || prices.length<1) {
            return maxProfit;
        }
        int leftMin = prices[0];
        for (int i = 0; i < prices.length; i++) {
            int price = prices[i];
            if (price<=leftMin){
                leftMin = price;
            }else if( (price-leftMin)>maxProfit){
                maxProfit =price - leftMin;
            }
        }

        return maxProfit;
    }

}
